local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.BaseComponent());
local Trailing_Cfg=ServerOHOH.config.Trailing
local PlayerCharacterHelper = _G.CommonOHOH.load.PlayerCharacterHelper();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    
    assert(self.FriendCtrl,"self.FriendCtrl==nil")
    
    self.FriendFolder=Instance.new("Folder",self.parent.main_inst)
    self.FriendFolder.Name="Friend"
    self.Equip={}
   local leftAtt= self.parent.main_inst.UpperTorso.LeftShoulderRigAttachment
   local rightAtt= self.parent.main_inst.UpperTorso.RightShoulderRigAttachment
    local df=2.5
    local dx=1.5

    local att1=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att1.Position=leftAtt.Position+Vector3.new(-dx,0,0)
    local att2=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att2.Position=rightAtt.Position+Vector3.new(dx,0,0)
    local att3=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att3.Position=leftAtt.Position+Vector3.new(-dx*2,0,0)
    local att4=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att4.Position=rightAtt.Position+Vector3.new(dx*2,0,0)


    local att5=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att5.Position=leftAtt.Position+Vector3.new(-dx,df,0)
    local att6=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att6.Position=rightAtt.Position+Vector3.new(dx,df,0)
    local att7=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att7.Position=leftAtt.Position+Vector3.new(-dx*2,df,0)
    local att8=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    att8.Position=rightAtt.Position+Vector3.new(dx*2,df,0)


    -- local att5=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    -- att5.Position=Vector3.new(df,df*2,df)
    -- local att6=Instance.new("Attachment",self.parent.main_inst.UpperTorso)
    -- att6.Position=Vector3.new(0,df*2,0)

    -- local att1=Instance.new("Attachment",self.parent.main_inst.LeftUpperArm)
    -- att1.Position=Vector3.new(0,df*1,0)
    -- local att2=Instance.new("Attachment",self.parent.main_inst.RightUpperArm)
    -- att2.Position=Vector3.new(0,df*1,0)
    -- local att3=Instance.new("Attachment",self.parent.main_inst.Head)
    -- att3.Position=Vector3.new(0,df*1,0)
    -- local att4=Instance.new("Attachment",self.parent.main_inst.LeftUpperArm)
    -- att4.Position=Vector3.new(df,df*2,df)
    -- local att5=Instance.new("Attachment",self.parent.main_inst.RightUpperArm)
    -- att5.Position=Vector3.new(df,df*2,df)
    -- local att6=Instance.new("Attachment",self.parent.main_inst.Head)
    -- att6.Position=Vector3.new(0,df*2,0)

    self.AttachData={
        [1]={
            att=att1,
            isUsing=false
        },
        [2]={
            att=att2,
            isUsing=false
        },
        [3]={
            att=att3,
            isUsing=false
        },

        [4]={
            att=att4,
            isUsing=false
        },
        [5]={
            att=att5,
            isUsing=false
        },
        [6]={
            att=att6,
            isUsing=false
        },
        [7]={
            att=att7,
            isUsing=false
        },
        [8]={
            att=att8,
            isUsing=false
        },
    }

    self.AttachUserId={}
    
    self:UpdateFriend()

end;    

function M.dtor(self)
	-- print("Server_AttachFriendCtrl--dtor----Server")

    for k,v in pairs(self.Equip) do
        v:dtor()
    end
    self.Equip={}

	M.super.dtor(self);

end;

function M.GetFriend(self,uid)
    return self.Equip[uid]
end

function M.GetAttachData(self,uid)

    for j,p in pairs(self.AttachData) do
        p.isUsing=false
    end

    for k,v in pairs(self.AttachUserId) do
        self.AttachData[v].isUsing=true
    end

    for j,p in pairs(self.AttachData) do
        if not p.isUsing then
            self.AttachUserId[uid]=j
            p.isUsing=true
            return p
        end
    end
end

function M.InitAttachData(self,uid,character)

    local d= self:GetAttachData(uid)
    assert(d,"Attach==null")
    character.Parent= self.FriendFolder
    character:SetPrimaryPartCFrame(d.att.WorldCFrame)
    -- local part=Instance.new("Part",character)
    -- part.Position=d.att.WorldPosition-Vector3.new(0,3,0)
    -- part.Size=Vector3.new(10,1,10)
    local w=Instance.new("WeldConstraint",character)
    w.Part0=character.PrimaryPart

    -- local w=Instance.new("WeldConstraint",part)
    -- w.Part0=part

    w.Part1=self.parent.main_inst.PrimaryPart

end


-- function M.Attack(self)
--     self:AddProcess(function()
--         for k,v in pairs(self.Equip) do
--             v:Attack()
--         end
--     end)
-- end
function M.UpdateFriend(self)
    self:AddProcess(function()
         local eq=self.FriendCtrl:GetEquipData()
         local removeIds={}
         local removeIdsEquip={}

         for uid,attId in pairs(self.AttachUserId) do
            if not eq[uid] then
                table.insert(removeIds,uid)
            end
         end

         for uid,v in pairs(self.Equip) do
            if not eq[uid] then
                table.insert(removeIdsEquip,uid)
            end
         end

         for _,id in pairs(removeIds) do
            self.AttachUserId[id]=nil
         end

         for _,id in pairs(removeIdsEquip) do
            self.Equip[id]:dtor()
            self.Equip[id]=nil
         end

         for uid,v in pairs(eq) do
            if not self.Equip[uid] then
                PlayerCharacterHelper.GetPlayerCharacter(uid,function(obj)
                    assert(obj,"CreateFriend obj=null")
                    if not self.FriendCtrl:GetEquipData()[uid] then
                        obj:Destroy()
                    elseif self.Equip[uid] then
                        obj:Destroy()
                    else
                        self:InitAttachData(uid,obj)
                        self.Equip[uid]=ServerOHOH.load.Server_AwakeFriendCharacter().new(obj,uid,self.parent)
                    end
         
                end)
            else
            end
         end
    end)

end


return M